package com.dongbao.util;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.*;

public class NetUtil {

    public static Boolean connect(String server, int servPort) {
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(server, servPort), 200);
            return true;
        } catch (IOException exception) {
            return false;
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                return false;
            }
        }
    }

    public static String sendUrlRequest(String urlStr) {
        return sendUrlRequest(urlStr,"POST");

    }

    public static String sendUrlRequest(String urlStr,String requestType) {
        String tempStr = null;
        HttpURLConnection url_con = null;
        try {
            URL url = new URL(urlStr);
            StringBuffer bankXmlBuffer = new StringBuffer();
            //创建URL连接，提交到数据，获取返回结果
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(requestType);
            connection.setDoOutput(true);
            connection.setRequestProperty("User-Agent", "directclient");
            connection.setReadTimeout(1000);
            connection.setConnectTimeout(500);

            PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "GBK"));
            out.close();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection
                    .getInputStream(), "GBK"));

            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                bankXmlBuffer.append(inputLine);
            }
            in.close();
            tempStr = bankXmlBuffer.toString();
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();

        } finally {
            if (url_con != null)
                url_con.disconnect();
        }

        return tempStr;

    }

    /**
     *
     * @param url 这个只支持http地址，不支持https
     * @param request
     * @return
     */

    public static String sendMessage(String url) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);  //url:请求地址 get请求
        httpGet.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8");
        try {
            HttpResponse res = httpClient.execute(httpGet);
            String rtn = EntityUtils.toString(res.getEntity());
            return rtn;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpGet.releaseConnection();
        }
        return null;
    }


}
